﻿function Resaltar_On(GridView) {
    if (GridView != null) {
        GridView.originalBgColor = GridView.style.backgroundColor;
        GridView.style.backgroundColor = "#FFD700";
    }
}

function Resaltar_Off(GridView) {
    if (GridView != null) {
        GridView.style.backgroundColor = GridView.originalBgColor;
    }
}

function ModalPopup(page, features, textboxValue, textBoxText) {
    var o = window.showModalDialog(page, null, features);
    if (o != null) {
        document.getElementById(textboxValue).value = o.value;
        document.getElementById(textBoxText).value = o.text;
        return false;
    }
    else
        return false;
}

function ModalPopup3(page, features, textboxValue, textBoxText, textBoxAux) {
    var o = window.showModalDialog(page, null, features);
    if (o != null) {
        document.getElementById(textboxValue).value = o.value;
        document.getElementById(textBoxText).value = o.text;
        document.getElementById(textBoxAux).value = o.aux;
        return false;
    }
    else
        return false;
}

function checkRutField(source, arguments) {
    var rut = arguments.Value.replace('.','').replace('-','');
    var tmpstr = "";
    for (i = 0; i < rut.length; i++)
        if (rut.charAt(i) != ' ' && rut.charAt(i) != '.' && rut.charAt(i) != '-')
        tmpstr = tmpstr + rut.charAt(i);
    rut = tmpstr;
    largo = rut.length;
    // [VARM+]
    tmpstr = "";
    for (i = 0; rut.charAt(i) == '0'; i++);
    for (; i < rut.length; i++)
        tmpstr = tmpstr + rut.charAt(i);
    rut = tmpstr;
    largo = rut.length;
    // [VARM-]
    if (largo < 2) {
        //alert("Debe ingresar el rut completo.");
        //document.frm.rut_aux.focus();
        //document.frm.rut_aux.select();
        arguments.IsValid = false;
        return;
    }
    for (i = 0; i < largo; i++) {
        if (rut.charAt(i) != "0" && rut.charAt(i) != "1" && rut.charAt(i) != "2" && rut.charAt(i) != "3" && rut.charAt(i) != "4" && rut.charAt(i) != "5" && rut.charAt(i) != "6" && rut.charAt(i) != "7" && rut.charAt(i) != "8" && rut.charAt(i) != "9" && rut.charAt(i) != "k" && rut.charAt(i) != "K") {
            //alert("El valor ingresado no corresponde a un R.U.T valido.");
            //document.frm.rut_aux.focus();
            //document.frm.rut_aux.select();
            arguments.IsValid = false;
            return;
        }
    }
    var invertido = "";
    for (i = (largo - 1), j = 0; i >= 0; i--, j++)
        invertido = invertido + rut.charAt(i);
    var drut = "";
    drut = drut + invertido.charAt(0);
    drut = drut + '-';
    cnt = 0;
    for (i = 1, j = 2; i < largo; i++, j++) {
        if (cnt == 3) {
            drut = drut + '.';
            j++;
            drut = drut + invertido.charAt(i);
            cnt = 1;
        }
        else {
            drut = drut + invertido.charAt(i);
            cnt++;
        }
    }
    invertido = "";
    for (i = (drut.length - 1), j = 0; i >= 0; i--, j++)
        invertido = invertido + drut.charAt(i);
    //document.frm.rut_aux.value = invertido;
    if (checkDV(rut)) {
        arguments.IsValid = true;
        return;
    }
    arguments.IsValid=false;
}

function checkDV(crut) {
    largo = crut.length;
    if (largo < 2) {
        //alert("Debe ingresar el rut completo.");
        //document.frm.rut_aux.focus();
        //document.frm.rut_aux.select();
        return false;
    }
    if (largo > 2)
        rut = crut.substring(0, largo - 1);
    else
        rut = crut.charAt(0);
    dv = crut.charAt(largo - 1);
    checkCDV(dv);
    if (rut == null || dv == null)
        return 0;
    var dvr = '0';
    suma = 0;
    mul = 2;
    for (i = rut.length - 1; i >= 0; i--) {
        suma = suma + rut.charAt(i) * mul;
        if (mul == 7)
            mul = 2;
        else
            mul++;
    }
    res = suma % 11;
    if (res == 1)
        dvr = 'k';
    else if (res == 0)
        dvr = '0';
    else {
        dvi = 11 - res;
        dvr = dvi + "";
    }
    if (dvr != dv.toLowerCase()) {
        //alert("EL rut es incorrecto.");
        //document.frm.rut_aux.focus();
        //document.frm.rut_aux.value = "";
        return false;
    }
    return true;
}
function checkCDV(dvr) {
    dv = dvr + "";
    if (dv != '0' && dv != '1' && dv != '2' && dv != '3' && dv != '4' && dv != '5' && dv != '6' && dv != '7' && dv != '8' && dv != '9' && dv != 'k' && dv != 'K') {
        //alert("Debe ingresar un digito verificador valido.");
        //document.frm.rut_aux.focus();
        //document.frm.rut_aux.select();
        return false;
    }
    return true;
}


function clickButton(e, buttonid) {

    var evt = e ? e : window.event;

    var bt = document.getElementById(buttonid);

    if (bt) {

        if (evt.keyCode == 13) {

            bt.click();

            return false;

        }

    }

}

///////////////////////JQUERY////////////////////////////////

       